<template>
  <div class="content">
    <p class="kong"></p>
    <div class="list" v-for="(item,index) in stateList" :key="index">
      <i-cell is-link i-class="listUse" @click="linkAward(item)">
        <img
          :src="item.shareType === 'consume'?'https://akk.028wkf.cn/kdb-weapp-img/img/qfx_xfyj.png':item.shareType === 'register' ?'https://akk.028wkf.cn/kdb-weapp-img/img/qfx_zcyj.png':'https://akk.028wkf.cn/kdb-weapp-img/img/qfx_lkyj.png'"
          alt
        />
        <span
          :class="item.state === '0'?'startUse':item.state === '1'?'endUse':'value1Bool2'"
        >{{item.state === '0'?'启用':item.state === '1'?'停用':'停止'}}</span>
      </i-cell>
    </div>
    <div class="list" v-for="(item,index) in addStateList" :key="index">
      <i-cell is-link i-class="listUse" @click="linkAward(item)">
        <img
          :src="item.shareType === 'consume'?'https://akk.028wkf.cn/kdb-weapp-img/img/qfx_xfyj.png':item.shareType === 'register' ?'https://akk.028wkf.cn/kdb-weapp-img/img/qfx_zcyj.png':'https://akk.028wkf.cn/kdb-weapp-img/img/qfx_lkyj.png'"
          alt
        />
        <span
          :class="item.state === '0'?'startUse':item.state === '1'?'endUse':'value1Bool2'"
        >{{item.state === '0' ? '启用': item.state === '1' ? '停用' : '新建'}}</span>
      </i-cell>
    </div>
  </div>
</template>

<script>
import { GET_SHOP_SHARE, FIND_BY_SHOPID_PAGEAPI } from '@/api'

export default {
  components: {},
  data () {
    return {
      stateList: [],
      addStateList: [],
      initStateList: [
        {
          balance: 0,
          balanceNum: 0,
          couponNum: 0,
          couponTypeId: 0,
          pension: 0,
          pensionNum: 0,
          rewardMan: '',
          shareTypeEnum: 'CONSUME',
          shareType: 'consume'
          // shopId: this.account.shopId
        },
        {
          balance: 0,
          balanceNum: 0,
          couponNum: 0,
          couponTypeId: 0,
          pension: 0,
          pensionNum: 0,
          rewardMan: '',
          shareTypeEnum: 'REGISTER',
          shareType: 'register'
          // shopId: this.account.shopId
        },
        {
          balance: 0,
          balanceNum: 0,
          couponNum: 0,
          couponTypeId: 0,
          pension: 0,
          pensionNum: 0,
          rewardMan: '',
          shareTypeEnum: 'PENSION_CARD',
          shareType: 'pensionCard'
          // shopId: this.account.shopId
        }
      ],
      isBuyShare: false
    }
  },
  computed: {
    account () {
      return this.$cookie.get('account')
    }
  },
  onShow () {
    this.addStateList = []
    this._findTool()
    this._getShopShare()
  },
  created () {},
  methods: {
    linkAward (val) {
      let link = ''
      let linkData = ''
      if (val.shareType === 'consume') {
        link = 'award-expense'
      } else if (val.shareType === 'register') {
        link = 'award-login'
      } else {
        link = 'award-card'
      }
      linkData = JSON.stringify(val)
      this.$router.push('/pages/home/' + link + '/main?linkData=' + linkData)
    },
    _getShopShare () {
      const vm = this
      GET_SHOP_SHARE(vm.account.shopId)
        .then(r => {
          if (r.data.code === '0') {
            if (!r.data.data.length) {
              vm.addStateList = vm.initStateList
              // vm.addStateList = r.data.data
            } else {
              let list = []
              vm.stateList = r.data.data
              vm.initStateList.forEach((e, i) => {
                vm.stateList.forEach((v, n) => {
                  if (e.shareType !== v.shareType) {
                    list.push(e)
                  }
                })
              })
              let add = list.filter(
                _ => !vm.stateList.some(e => e.shareType === _.shareType)
              )
              let result = []
              let obj = {}
              for (let i = 0; i < add.length; i++) {
                if (!obj[add[i].shareType]) {
                  result.push(add[i])
                  obj[add[i].shareType] = true
                }
              }
              vm.addStateList = result
            }
          }
        })
        .catch(e => {
          console.info(e)
        })
    }, // 获取商户信息
    _findTool () {
      const vm = this
      FIND_BY_SHOPID_PAGEAPI(vm.account.shopId, 1, 99)
        .then(r => {
          if (r.data.code === '0') {
            let state = 0
            let data = [...r.data.data.list]
            for (let i = 0; i < data.length; i++) {
              if (data[i].marketingType === 'SHARE') {
                state = 1
                break
              } else {
                state = 0
              }
            }
            if (!state) {
              vm
                .$dialog('尚未购买趣分享，前往购买', '')
                .then(() => {
                  vm.$router.push(
                    '/pages/shop-management/marketing-buy-tool/main'
                  )
                })
                .catch(() => {
                  vm.$router.go(-1)
                })
            }
          } else {
            vm.$toast(r.data.message)
          }
        })
        .catch(e => {
          console.info(e)
        })
    }
  }
}
</script>

<style lang="scss">
.content {
  background: #eef2f5;
  .kong {
    padding-top: 30px;
    background: #eef2f5;
  }
  .list {
    margin: 10px 0;
    .listUse {
      position: relative;
      height: 20px;
      img {
        width: 50px;
        height: 25px;
        position: absolute;
        top: 50%;
        left: 40px;
        transform: translate(-50%, -50%);
      }
      span {
        width: 46px;
        text-align: right;
        position: absolute;
        top: 50%;
        right: 16px;
        transform: translate(-50%, -50%);
        font-size: 12px;
      }
      .startUse {
        color: #487a33 !important;
      }
      .endUse {
        color: #a6a6a6 !important;
      }
      .endUse {
        color: #ab0f3a !important;
      }
    }
  }
}
</style>

